package com.haiyou.data.common.accessor.redis.redisson.bucket;

import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import com.google.common.collect.Sets;
import com.haiyou.common.constants.ErrCode;
import com.haiyou.common.entity.ServerEntity;
import com.haiyou.common.util.check.Assert;
import com.haiyou.data.game.base.GamePlayerModuleCaster;

public class BucketServerAccessor<E extends ServerEntity<String>> extends BucketAccessor<E>{
	

	public E compute(int serverId, BiFunction<? super String, ? super E, ? extends E> remappingFunction) {
		String key = GamePlayerModuleCaster.castServerIdTo(serverId, getValueClass()); 
		return super.compute(key, remappingFunction);
	}	
	
	
	public E get(int serverId) {
		String key = GamePlayerModuleCaster.castServerIdTo(serverId, getValueClass());
		return super.get(key);
	}
	

	public E get(int serverId,Function<? super String, ? extends E> mappingFunction) {
		String key = GamePlayerModuleCaster.castServerIdTo(serverId, getValueClass());
		return super.get(key,mappingFunction);
	}

	public E getRedis(int serverId) {
		String key = GamePlayerModuleCaster.castServerIdTo(serverId, getValueClass());
		return super.getRedis(key);
	}
	
	
	public Map<String, E> getAllByServerIds(Iterable<? extends Integer> serverIds) {
		Set<String> keySet = Sets.newHashSet();
		serverIds.forEach(key -> {
				keySet.add(GamePlayerModuleCaster.castServerIdTo(key, getValueClass()));
		});

		return super.getAll(keySet);
	}

	@Override
	public String generateKey() {
		Assert.isTrue(false, ErrCode.METHOD_NOT_SUPPORT);
		return null;
	}

	@Override
	public E generate(Consumer<E> consumer) {
		Assert.isTrue(false, ErrCode.METHOD_NOT_SUPPORT);
		return null;
	}

	
	public String generateKey(int serverId) {
		return GamePlayerModuleCaster.castServerIdTo(serverId, getValueClass());
	}
	
	
}
